package com.yx.cloud.activity;

import android.view.View;

import com.google.gson.Gson;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.GetCleanRecordDetailParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.databinding.CleanrecordDetailActivityBinding;
import com.yx.cloud.map.YXMapView;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPrefsUtil;
import com.iot.product.sweeper.bean.CleanRecord;
import com.iot.common.utils.Event;
import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * Created by Administrator on 2019/2/21.
 */

public class CleanRecordDetailActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "RecordDetailActivity";

    private String recordData;
    private YXMapView yxMapView;
    private CleanrecordDetailActivityBinding binding;

    @Override
    public void onCreate() {
        binding = CleanrecordDetailActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
        initMapView();
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        recordData = SharedPrefsUtil.getValue(this, "cleanRecordData", "recordData", "");
        Gson gson = new Gson();
        CleanRecord cleanRecordBean = gson.fromJson(recordData, CleanRecord.class);
        if (cleanRecordBean != null) {
            initWidget(cleanRecordBean);
        }
    }

    private void initData() {
        binding.ivCleanDetailReturn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_clean_detail_return:
                finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_GET_CLEAN_RECORD_DETAIL:
                List<CleanRecord> records = (List<CleanRecord>) event.getContent();
                yxMapView.initMap(records.get(0).getMap());
                break;
        }
    }


    private void initMapView() {
        yxMapView = findViewById(R.id.targetView);
    }

    private void initWidget(CleanRecord recordData) {
        GetCleanRecordDetailParam param = new GetCleanRecordDetailParam(recordData.getId());
        param.setCleanRecord(recordData);
        AppContext.getProductService().getCleanRecordDetail(param);

        String startTime = recordData.getStartTime();
        binding.tvCleanStartTime.setText(startTime);

        int sweepArea = recordData.getArea();
        binding.tvCleaningArea.setText(sweepArea + "");

        int sweepTime = recordData.getCleanTime();
        binding.tvCleaningTime.setText(sweepTime + "");

        int cleanMode = recordData.getCleanMode();
        switch (cleanMode) {
            case Constants.CLEAN_MODE_DOT:
                binding.tvCleanMode.setText(getString(R.string.U_SpotClean));
                break;
            case Constants.CLEAN_MODE_AREA:
                binding.tvCleanMode.setText(getString(R.string.U_RoomClean));
                break;
            case Constants.CLEAN_MODE_DOT_YUDI:
                binding.tvCleanMode.setText(getString(R.string.U_SpotClean));
                break;
            case Constants.CLEAN_MODE_ROOM:
                binding.tvCleanMode.setText(getString(R.string.U_RoomClean));
                break;
            case Constants.CLEAN_MODE_AUTO_SPLIT_ROOM:
                //binding.tvCleanMode.setText(getString(R.string.Intelligent_partition_cleaning));
                break;
            case Constants.CLEAN_MODE_DEPTH:
                //binding.tvCleanMode.setText(getString(R.string.Deep_cleaning));
                break;
            case Constants.CLEAN_MODE_EXTEND:
                //binding.tvCleanMode.setText(getString(R.string.Yanbian_cleaning));
                break;
            case Constants.CLEAN_MODE_DEMO:
                //binding.tvCleanMode.setText(getString(R.string.demo_mode));
                break;
            case Constants.CLEAN_MODE_EXPLORE:
                //binding.tvCleanMode.setText(getString(R.string.Explore_mode));
                break;
            case Constants.CLEAN_MODE_ALL:
            default:
                binding.tvCleanMode.setText(getString(R.string.U_AutoClean));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        yxMapView.setRoomTagMode(false, false, false, false, false, false, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        yxMapView.setRoomTagMode(true, false, false, false, false, false, false);
    }
}
